package com.study.thread;

/**
 * @Description: 可重入锁
 * @Author King
 * @Date 2024/12/15 16:49
 */
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockExample {
    private final ReentrantLock lock = new ReentrantLock();

    public void outerMethod() {
        lock.lock();
        try {
            System.out.println("Outer method acquired the lock.");
            innerMethod(); // 调用内部方法
        } finally {
            lock.unlock();
        }
    }

    public void innerMethod() {
        lock.lock();
        try {
            System.out.println("Inner method acquired the lock.");
            // 执行一些操作...
        } finally {
            lock.unlock();
        }
    }
    public void outerMethod2(ReentrantLockExample re) {
        synchronized (re) {
            System.out.println("进入 out 方法");
            innerMethod2(re);
        }
    }
    public void innerMethod2(ReentrantLockExample re) {
        synchronized (re) {
            System.out.println("进入 inner 方法");
        }
    }

    public static void main(String[] args) {
        ReentrantLockExample example = new ReentrantLockExample();
//        example.outerMethod();
        example.outerMethod2(example);
    }
}
